package controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/12/6 9:48
 */
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先找到当前用户会话
        HttpSession session = req.getSession(false);
        if(session == null){
            //没登录
            resp.getWriter().write("请先登录！");
            return;
        }
        //删除用户会话中的信息就达到注销功能
        session.removeAttribute("user");//破坏user
        resp.sendRedirect("blog_login.html");
    }
}
